package ui.activity.mine;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import ui.activity.mine.biz.ChangePwdBiz;
import ui.activity.mine.component.DaggerChangePwdComponent;
import ui.activity.mine.contract.ChangePwdContract;
import ui.activity.mine.module.ChangePwdModule;
import ui.activity.mine.presenter.ChangePwdPresenter;
import widget.ClearEditText;


/**
 * 修改密码
 * Created by chenjian on 2017/8/4.
 */

public class ChangePwdAct extends BaseAct implements ChangePwdContract.View {


    @Inject
    ChangePwdPresenter presenter;
    @Inject
    ChangePwdBiz biz;

    @BindView(R.id.changepass_edit1)
    ClearEditText oldPwd;
    @BindView(R.id.changepass_edit2)
    ClearEditText newPwd;
    @BindView(R.id.changepass_edit3)
    ClearEditText surePwd;
    @BindView(R.id.changepass_but)
    Button submit;

    private SharedPreferences sp;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_changepassword);
    }

    @Override
    protected void setup() {
        DaggerChangePwdComponent.builder().changePwdModule(new ChangePwdModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_edit_pwd), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.setPwd(sp.getString("id", "0"), oldPwd.getText().toString().trim(), newPwd.getText().toString().trim(), surePwd.getText().toString().trim()));
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

}
